package com.eaoa.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.constants.PubMessageConstants;
import com.eaoa.query.pub.PubMessageQuery;
import com.eaoa.vo.pub.PubAccount;
import com.stony.core.bo.HibernateBusinessObject;
import com.stony.core.exception.HttpSessionException;
import com.stony.core.struts.action.BaseAction;

public abstract class EABaseAction extends BaseAction {

	static private List lv = new ArrayList();
	static {
		lv.add(new Long(-1));
	}

	protected PubAccount getEmployee(HttpServletRequest request) {
		return (PubAccount) request.getSession().getAttribute("Employee");
	}

	protected Collection getCustomernos(HttpServletRequest request) {
		PubAccount employee = getEmployee(request);
		return employee.getCustomerids();
	}
	
	protected boolean hasPermission(HttpServletRequest request, Long val) {
		PubAccount employee = getEmployee(request);
		return employee.getPermissionids().contains(val);
	}

	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (getEmployee(request) == null)
			throw new HttpSessionException();
		
		PubMessageQuery messageQuery = new PubMessageQuery();
		messageQuery.setReceiver(getEmployee(request).getAccountno());
		messageQuery.setIslock("N");
		messageQuery.setStatus(PubMessageConstants.MESSAGE_STATUS_NRD);
		//messageQuery.execute();
		request.setAttribute("messagesNumber", messageQuery.getResultsCount());
		
		HibernateBusinessObject.EMPLOYEE.set(getEmployee(request));
		ActionForward forward = doWork(mapping, form, request, response);
		return forward;
	}

	protected abstract ActionForward doWork(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception;
}
